<template>
  <div class="create_order_1 w1200">
    <div class="step_bar">
      <div class="step">
        <div class="item check">
          <el-icon><Shop /></el-icon>
          我的购物车
        </div>
        <div class="item">
          <el-icon><MessageBox /></el-icon>
          物流地址
        </div>
        <div class="item">
          <el-icon><Money /></el-icon>
          选择支付
        </div>
        <div class="item">
          <el-icon><CircleCheckFilled /></el-icon>
          支付成功
        </div>
      </div>
    </div>
    <div class="xtx-cart-page">
      <div class="container m-top-20">
        <div class="cart">
          <table>
            <thead>
              <tr>
                <th width="120">
                  <el-checkbox :model-value="cartStore.isAllCheck" @change="allCheck" />
                </th>
                <th width="400">商品信息</th>
                <th width="220">单价</th>
                <th width="180">数量</th>
                <th width="180">小计</th>
                <th width="140">操作</th>
              </tr>
            </thead>
            <!-- 商品列表 -->
            <tbody>
              <tr v-for="i in cartStore.cartList" :key="i.id">
                <td>
                  <el-checkbox :model-value="i.selected" @change="(selected: boolean) => singleCheck(selected, i.id)" />
                </td>
                <td>
                  <div class="goods">
                    <RouterLink to="/">
                      <img :src="getFirstImageUrl(i.skuImgUrl)" alt="" />
                    </RouterLink>
                    <div>
                      <p class="name ellipsis">
                        {{ i.skuName || i.goodsName }}
                      </p>
                    </div>
                  </div>
                </td>
                <td class="tc">
                  <p>&yen;{{ i.price }}</p>
                </td>
                <td class="tc">
                  <el-input-number v-model="i.count" />
                </td>
                <td class="tc">
                  <p class="f16 red">&yen;{{ (i.price * i.count).toFixed(2) }}</p>
                </td>
                <td class="tc">
                  <p>
                    <el-popconfirm title="确认删除吗?" confirm-button-text="确认" cancel-button-text="取消" @confirm="cartStore.delCart(i)">
                      <template #reference>
                        <a href="javascript:;">删除</a>
                      </template>
                    </el-popconfirm>
                  </p>
                </td>
              </tr>
              <tr v-if="cartStore.cartList.length === 0">
                <td colspan="6">
                  <div class="cart-none">
                    <el-empty description="购物车列表为空">
                      <el-button type="primary">随便逛逛</el-button>
                    </el-empty>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <!-- 操作栏 -->
        <div class="action">
          <div class="batch">
            共 {{ cartStore.totalCount }} 件商品，已选择 {{ cartStore.selectedCount }} 件，商品合计：
            <span class="red">¥ {{ cartStore.selectedMoney.toFixed(2) }}</span>
          </div>
          <div class="total">
            <!--            <el-button size="large" type="primary" @click="$router.push('/user/order/before')">下单结算</el-button>-->
            <el-button size="large" type="primary" @click="buy">下单结算</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useCartStore } from '@/store/modules/carts';
import { Shop, MessageBox, Money, CircleCheckFilled } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';

const cartStore = useCartStore();
const router = useRouter();
/*单选框*/
const singleCheck = (selected: boolean, id: string) => {
  cartStore.singleCheck(id, selected);
};
/**多选框选中数据*/
const allCheck = (selected: boolean) => {
  cartStore.allCheck(selected);
};
/*取第一张图片*/
const getFirstImageUrl = (imageUrls: string) => {
  const image = imageUrls ? imageUrls.split(',') : '';
  return image.length > 0 ? image[0] : '@/assets/images/none.png';
};
/*下单结算*/
const buy = () => {
  //TODO 判断这个订单中的商品是否涉及有多个租户，如果有，则需要将订单进行拆分
  let params: any = {
    order: [],
    ifCart: 1 // 是否购物车
  };
  // 检查是否有选中的商品
  if (cartStore.selectedCount <= 0) return ElMessage.error('未选择商品');
  // 遍历cartStore.cartList数组，筛选出selected=true的对象
  cartStore.cartList.forEach((item: any) => {
    if (item.selected) {
      params.order.push(item);
    }
  });
  // 将params对象转换为JSON字符串，并进行URL编码
  let str = encodeURIComponent(JSON.stringify(params));
  // 通过router.push把参数传递给订单结算页面
  router.push('/user/order/before/' + str);
};
</script>
<style lang="scss" scoped>
.step_bar {
  margin: 40px 0;

  .step {
    height: 46px;
    line-height: 46px;
    background: #f5f7fa;
    margin-bottom: 50px;
    display: flex;
  }

  .step .item {
    font-size: 16px;
    color: #c0c4cc;
    flex: 1;
    justify-content: center;
    align-items: center;
    display: flex;

    i {
      margin-right: 10px;
    }

    text-align: center;
    border-right: 4px solid #fff;
  }

  .step .item.check {
    color: #333;
    font-weight: bold;
  }
}

.xtx-cart-page {
  margin-top: 20px;

  .cart {
    background: #fff;
    color: #666;

    table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 24px;

      th,
      td {
        padding: 10px;
        border-bottom: 1px solid #f5f5f5;

        &:first-child {
          text-align: left;
          padding-left: 30px;
          color: #999;
        }
      }

      th {
        font-size: 16px;
        font-weight: normal;
        line-height: 50px;
      }
    }
  }

  .cart-none {
    text-align: center;
    padding: 120px 0;
    background: #fff;

    p {
      color: #999;
      padding: 20px 0;
    }
  }

  .tc {
    text-align: center;

    a {
      color: $xtxColor;
    }

    .xtx-numbox {
      margin: 0 auto;
      width: 120px;
    }
  }

  .red {
    color: $priceColor;
  }

  .green {
    color: $xtxColor;
  }

  .f16 {
    font-size: 16px;
  }

  .goods {
    display: flex;
    align-items: center;

    img {
      width: 100px;
      height: 100px;
    }

    > div {
      width: 280px;
      font-size: 16px;
      padding-left: 10px;

      .attr {
        font-size: 14px;
        color: #999;
      }
    }
  }

  .action {
    display: flex;
    background: #fff;
    margin-top: 20px;
    height: 80px;
    align-items: center;
    font-size: 16px;
    justify-content: space-between;
    padding: 0 30px;

    .xtx-checkbox {
      color: #999;
    }

    .batch {
      a {
        margin-left: 20px;
      }
    }

    .red {
      font-size: 18px;
      margin-right: 20px;
      font-weight: bold;
    }
  }

  .tit {
    color: #666;
    font-size: 16px;
    font-weight: normal;
    line-height: 50px;
  }
}
</style>
